// pages/balanceDetail/balanceDetail.js
var appData = getApp().globalData;
var common = require("../../utils/Conmon.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    open: false,
    SearchType: -1,
    Type: 0,
    TotalIn: 0,
    TotalOut: 0,
    LstDetail: [],
    showLoading: false,
    Title: '余额明细',
    Text: '全部',
    bgTime: "2010-01-01",
    endTime: ""
  },
  openDestination: function() {
    this.setData({
      open: !this.data.open
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if (month < 10) {
      month = "0" + month;
    }
    if (day < 10) {
      day = "0" + day;
    }
    var nowDate = year + "-" + month + "-" + day;
    this.setData({
      endTime: nowDate
    })
    common.pageInit(function() {});
    var that = this;
    var _type = options.typeId;
    var _title = options.name;
    if (_title != undefined) {
      that.setData({
        Type: _type,
        Title: _title + '明细'
      })
    }

    wx.setNavigationBarTitle({
      title: that.data.Title
    }); //修改title
    var param = {
      custId: appData.custID,
      userId: wx.getStorageSync('UserInfo').UserID,
      typeId: that.data.Type,
      bgTime: that.data.bgTime,
      endTime: that.data.endTime
    };
    that.ajaxLoadData(param);
  },

  /**
   * 点击tab加载数据
   */
  bindLoadClickTap: function(event) {
    var _searchType = event.currentTarget.dataset.type;
    var selectText = event.currentTarget.dataset.text;
    var that = this;
    that.setData({
      SearchType: _searchType,
      Text: selectText
    })
    that.LoadDetailLog(_searchType);
  },

  /**
   * 请求获取数据
   */
  ajaxLoadData: function(param) {
    var that = this;
    wx.request({
      url: appData.apiurl + 'mobile/api/Commission/GetCommissonCount',
      method: 'GET',
      data: param,
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName
      },
      success: function(res) {
        if (res.statusCode == 200 && res.data.code == 1200) {
          var data = res.data;
          if (data.content != null) {
            that.setData({
              TotalIn: data.content.TotalIn == null ? 0 : data.content.TotalIn,
              TotalOut: data.content.TotalOut == null ? 0 : data.content.TotalOut
            })
          }
          that.LoadDetailLog(0)
          console.log(res.data)
        }
      },
      fail: function(res) {
        wx.showModal({
          title: '错误',
          content: '加载失败...',
          showCancel: false
        })
        console.log(res.data)
      }
    })
  },

  /**
   * 加载币种明细
   */
  LoadDetailLog: function(searchType) {
    var that = this;
    //显示加载中...
    that.setData({
      showLoading: true,
      LstDetail: [],
      open: false
    })
    var searchData = {
      custId: appData.custID,
      userId: wx.getStorageSync('UserInfo').UserID,
      typeId: that.data.Type,
      searchType: searchType == -1 ? 0 : searchType,
      bgTime: that.data.bgTime,
      endTime: that.data.endTime
      
    };
    wx.request({
      url: appData.apiurl + 'mobile/api/Commission/ListLogByTime',
      method: 'GET',
      data: searchData,
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName
      },
      success: function(res) {
        //隐藏加载
        that.setData({
          showLoading: false
        })
        if (res.statusCode == 200 && res.data.code == 1200) {
          var data = res.data;
          if (data.content != null) {
            that.setData({
              LstDetail: data.content
            })
          }
          console.log(res.data)
        }
      },
      fail: function(res) {
        //隐藏加载
        that.setData({
          showLoading: false
        });
        wx.showModal({
          title: '错误',
          content: '加载失败...',
          showCancel: false
        })
        console.log(res.data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  bindTimeChange: function(e) {
    var t = e.currentTarget.dataset.type;
    if (t == "bgTime") {
      this.setData({
        bgTime: e.detail.value
      })
    } else if (t == "endTime") {
      this.setData({
        endTime: e.detail.value
      })
    }

    this.LoadDetailLog(this.data.SearchType);


  }
})